using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace AvaloniaDrawingPad.Models
{
    public class Layer
    {
        public string Name { get; set; }
        public bool IsVisible { get; set; } = true;
        public Canvas Content { get; }
        public bool IsSelected { get; set; }

        public Layer(string name)
        {
            Name = name;
            Content = new Canvas
            {
                Background = Brushes.Transparent
            };
        }
    }

    public class LayerManager
    {
        private readonly ObservableCollection<Layer> _layers = new ObservableCollection<Layer>();
        private Layer? _selectedLayer;
        private readonly Canvas _mainCanvas;
        
        // 添加项目变更事件
        public event EventHandler? ProjectChanged;

        public ObservableCollection<Layer> Layers => _layers;

        public Layer? SelectedLayer
        {
            get => _selectedLayer;
            set
            {
                if (_selectedLayer != value)
                {
                    if (_selectedLayer != null)
                    {
                        _selectedLayer.IsSelected = false;
                    }

                    _selectedLayer = value;

                    if (_selectedLayer != null)
                    {
                        _selectedLayer.IsSelected = true;
                    }
                }
            }
        }

        public LayerManager(Canvas mainCanvas)
        {
            _mainCanvas = mainCanvas;
            // 创建默认图层
            AddLayer("背景");
        }

        public Layer AddLayer(string name)
        {
            var layer = new Layer(name);
            _layers.Add(layer);
            _mainCanvas.Children.Add(layer.Content);
            SelectedLayer = layer;
            return layer;
        }

        public void RemoveLayer(Layer layer)
        {
            if (_layers.Count <= 1)
            {
                // 至少保留一个图层
                return;
            }

            int index = _layers.IndexOf(layer);
            _layers.Remove(layer);
            _mainCanvas.Children.Remove(layer.Content);

            // 选择新的图层
            if (_layers.Count > 0)
            {
                int newIndex = Math.Min(index, _layers.Count - 1);
                SelectedLayer = _layers[newIndex];
            }
            else
            {
                SelectedLayer = null;
            }
        }

        public void MoveLayerUp(Layer layer)
        {
            int index = _layers.IndexOf(layer);
            if (index < _layers.Count - 1)
            {
                _layers.Move(index, index + 1);
                
                // 更新Canvas中的Z顺序
                _mainCanvas.Children.Remove(layer.Content);
                _mainCanvas.Children.Insert(_mainCanvas.Children.Count - (_layers.Count - 1 - index), layer.Content);
            }
        }

        public void MoveLayerDown(Layer layer)
        {
            int index = _layers.IndexOf(layer);
            if (index > 0)
            {
                _layers.Move(index, index - 1);
                
                // 更新Canvas中的Z顺序
                _mainCanvas.Children.Remove(layer.Content);
                _mainCanvas.Children.Insert(_mainCanvas.Children.Count - (_layers.Count - 1 - (index - 1)), layer.Content);
            }
        }

        public void ToggleLayerVisibility(Layer layer)
        {
            layer.IsVisible = !layer.IsVisible;
            layer.Content.IsVisible = layer.IsVisible;
        }

        public void ClearSelectedLayer()
        {
            if (SelectedLayer != null)
            {
                SelectedLayer.Content.Children.Clear();
            }
        }

        public void ClearAllLayers()
        {
            foreach (var layer in _layers)
            {
                layer.Content.Children.Clear();
            }
        }

        // 添加插入图层方法，用于撤销/重做
        public void InsertLayer(int index, Layer layer)
        {
            if (index < 0 || index > _layers.Count)
            {
                index = _layers.Count;
            }
            
            _layers.Insert(index, layer);
            
            // 计算在Canvas中的位置
            int canvasIndex = _mainCanvas.Children.Count - (_layers.Count - 1 - index);
            canvasIndex = Math.Max(0, Math.Min(canvasIndex, _mainCanvas.Children.Count));
            
            _mainCanvas.Children.Insert(canvasIndex, layer.Content);
            SelectedLayer = layer;
        }
        
        // 标记项目已修改
        public void MarkAsChanged()
        {
            ProjectChanged?.Invoke(this, EventArgs.Empty);
        }
    }
} 